/*
 * VerbruikTest.cpp
 *
 * Created on: 19-04-2014
 *     Author: A.W. Janisse
 */

#include "gtest/gtest.h"
#include "VerbruikTest.h"
#include "TestSensor.h"
#include "KlokObserver.h"
#include "SensorObserver.h"

TEST(VerbruiksMeting, Constructor)
{
    VerbruiksMeting *v = new VerbruiksMeting(new Display, new TestOpslag);

    ASSERT_TRUE(v != NULL);
    delete v;
}

TEST(VerbruiksMeting, Is_an_Observer)
{
    VerbruiksMeting *v = new VerbruiksMeting(new Display, new TestOpslag);
    TestSensor *s = new TestSensor();
    s->attachObserver(v);

    EXPECT_EQ( 0 , v->getUurTotaal() );

    s->tick();

    EXPECT_EQ( 1 , v->getUurTotaal() );

    delete s;
    delete v;
}

TEST(TestKlok, Constructor)
{
    TestKlok *klok = new TestKlok();

    ASSERT_TRUE(klok != NULL);
    delete klok;
}


TEST(TestKlok, Has_right_date_and_time)
{
    struct tm *now;
    time_t t;
    time(&t);
    now = localtime(&t);

    TestKlok *klok = new TestKlok();

    EXPECT_EQ( now->tm_sec, klok->getSecond() );
    EXPECT_EQ( now->tm_min, klok->getMinute() );
    EXPECT_EQ( now->tm_hour, klok->getHour() );

    EXPECT_EQ( now->tm_mday, klok->getDay() );
    EXPECT_EQ( now->tm_mon+1, klok->getMonth() );
    EXPECT_EQ( now->tm_year+1900, klok->getYear() );

    delete klok;
}

TEST(TestKlok, Notify_Multiple_Observer)
{
    TestKlok *klok = new TestKlok();

    KlokObserver *ko1 = new KlokObserver();
    KlokObserver *ko2 = new KlokObserver();
    KlokObserver *ko3 = new KlokObserver();

    klok->attachObserver(ko1);
    klok->attachObserver(ko2);
    klok->attachObserver(ko3);

    EXPECT_EQ( ko1->getIsUpdatet(), false );
    EXPECT_EQ( ko2->getIsUpdatet(), false );
    EXPECT_EQ( ko3->getIsUpdatet(), false );

    klok->tick();

    EXPECT_EQ( ko1->getIsUpdatet(), true );
    EXPECT_EQ( ko2->getIsUpdatet(), true );
    EXPECT_EQ( ko3->getIsUpdatet(), true );

    delete ko1;
    delete ko2;
    delete ko3;
    delete klok;
}

TEST(TestSensor, Constructor)
{
    TestSensor *sensor = new TestSensor();

    ASSERT_TRUE(sensor != NULL);
    delete sensor;
}

TEST(TestSensor, Notify_Multiple_Observer)
{
    TestSensor *sensor = new TestSensor();

    SensorObserver *so1 = new SensorObserver();
    SensorObserver *so2 = new SensorObserver();
    SensorObserver *so3 = new SensorObserver();

    sensor->attachObserver(so1);
    sensor->attachObserver(so2);
    sensor->attachObserver(so3);

    EXPECT_EQ( so1->getIsUpdatet(), false );
    EXPECT_EQ( so2->getIsUpdatet(), false );
    EXPECT_EQ( so3->getIsUpdatet(), false );

    sensor->tick();

    EXPECT_EQ( so1->getIsUpdatet(), true );
    EXPECT_EQ( so2->getIsUpdatet(), true );
    EXPECT_EQ( so3->getIsUpdatet(), true );

    delete so1;
    delete so2;
    delete so3;
    delete sensor;
}
